function splitString(str) {
  // 先将字符串每两个字符分为一组
  const twoCharGroups = delstr(str);

  const rules = [2, 2, 1, 2, 1, 4, 6, 2];
  const result = [];
  let index = 0;

  // 按照固定规则分割
  for (let rule of rules) {
    if (index + rule <= twoCharGroups.length) {
      result.push(twoCharGroups.slice(index, index + rule).join(''));
      index += rule;
    } else {
      break;
    }
  }

  // 处理 n 部分
  if (twoCharGroups.length - index > 2) {
    result.push(twoCharGroups.slice(index, twoCharGroups.length - 2).join(''));
    index = twoCharGroups.length - 2;
  }

  // 处理最后的长度为 2 的部分
  if (index + 2 <= twoCharGroups.length) {
    result.push(twoCharGroups.slice(index, index + 2).join(''));
  }

  return result;
}

function delstr(str) {
  const parts = [];
  for (let i = 0; i < str.length; i += 2) {
    parts.push(str.slice(i, i + 2));
  }
  return parts;
}

function Verify(str1) {
  console.log('str1', str1);
  let firstPart = str1.substring(0, 2);
  // 提取后两个字符
  let secondPart = str1.substring(2);
  console.log('firstPart', firstPart);
  console.log('secondPart', secondPart);
  // 交换顺序并拼接
  let result = secondPart + firstPart;
  console.log('result', result);
  let decimalNumber = parseInt(result, 16);
  console.log('parseInt(result, 16)', parseInt(result, 16));
  return decimalNumber;
}

// 示例输入
// const input = arr1[8];
// const output = sanlingqi(input);
// console.log(output);
// 测试示例
export function displayInput() {
  const inputString = document.getElementById('inputText').value;
  const displayArea = document.getElementById('displayArea');
  const verificationMap = {
    307: sanlingqi,
    101: yilingyi,
    102: yilinger,
    201: erlingyi,
    202: erlinger,
    203: erlingsan,
    204: erlingsi,
    301: sanlingyi,
    302: sanlinger,
    304: sanlingsi,
    305: sanlingwu,
    306: sanlingliu,
    308: sanlingba,
    309: sanlingjiu,
    310: sanyiling,
    401: silingyi,
    402: silinger,
    403: silingsan,
    404: silingsi,
    501: wulingyi,
    502: wulinger,
    503: wulingsan,
    504: wulingsi,
    505: wulingwu,
    506: wulingliu,
    507: wulingqi,
    508: wulingba,
    509: wulingjiu,
    510: wuyiling,
    601: liulingyi,
    602: liulinger,
    611: liuyiyi,
    612: liuyier,
    613: liuyisan,
    614: liuyisi,
    621: liueryi,
    622: liuerer,
    631: liusanyi,
    632: liusaner,
    637: liusanqi,
    638: liusanba,
    701: qilingyi,
  };
  if (inputString) {
    const arr1 = splitString(inputString);
    console.log('arr1', arr1);
    displayArea.textContent = Verify(arr1[3]);
    let buttonData = [];
    const verificationResult = Verify(arr1[3]);
    buttonData = verificationMap[verificationResult]
      ? verificationMap[verificationResult](arr1[8])
      : null;
    // 循环二维数组生成按钮

    if (buttonData) {
      showInfo(buttonData);
    }
  } else {
    resetForm();
  }
}

function showInfo(items) {
  const extraDisplayAreas = document.getElementById('extraDisplayAreas');
  if (extraDisplayAreas) {
    // 清空之前的内容
    extraDisplayAreas.innerHTML = '';
    // 创建表格元素
    const table = document.createElement('table');
    table.classList.add('data-table');
    // 创建表格头部
    const thead = document.createElement('thead');
    const headerRow = document.createElement('tr');
    // 假设 items 是二维数组，第一行数据作为表头
    if (items.length > 0) {
      for (let i = 0; i < items[0].length; i++) {
        const th = document.createElement('th');
        th.textContent = items[0][i];
        headerRow.appendChild(th);
      }
    }
    thead.appendChild(headerRow);
    table.appendChild(thead);
    // 创建表格主体
    const tbody = document.createElement('tbody');
    for (let i = 1; i < items.length; i++) {
      const row = document.createElement('tr');
      for (let j = 0; j < items[i].length; j++) {
        const td = document.createElement('td');
        td.textContent = items[i][j];
        row.appendChild(td);
      }
      tbody.appendChild(row);
    }
    table.appendChild(tbody);
    // 将表格添加到显示区域
    extraDisplayAreas.appendChild(table);
  }
}

function tongyong(input, rules, infoArray, type = 0) {
  const parts = delstr(input);
  const result = [];
  let index = 0;
  for (let rule of rules) {
    if (index + rule <= parts.length) {
      result.push(parts.slice(index, index + rule).join(''));
      index += rule;
    } else {
      break;
    }
  }
  const twoDArray = [];
  let rule = [];
  console.log(type);
  if (type == 307) {
    rule = [2, 4, 4, 4];
    for (let i = 0; i < result.length; i++) {
      result[i] = insertSpaces(result[i], rule);
    }
  } else if (type == 612) {
    rule = [34, 8];
    for (let i = 0; i < infoArray.lengthl; i++) {
      result[i] = insertSpaces(result[i], rule);
    }
  }
  for (let i = 0; i < infoArray.length; i++) {
    const row = [infoArray[i], result[i]];
    twoDArray.push(row);
  }
  return twoDArray;
}

function insertSpaces(str, rules) {
  let result = '';
  let index = 0;

  for (let i = 0; i < rules.length; i++) {
    if (index + rules[i] <= str.length) {
      result += str.slice(index, index + rules[i]);
      index += rules[i];
      if (i < rules.length - 1) {
        result += '  ';
      }
    } else {
      // 如果剩余字符串长度不足规则要求，直接添加剩余部分
      result += str.slice(index);
      break;
    }
  }
  return result;
}
export function resetForm() {
  // 清空输入框
  document.getElementById('inputText').value = '';
  // 清空显示区域
  document.getElementById('displayArea').textContent = '';
  // 清空额外显示区域
  const extraDisplayAreas = document.getElementById('extraDisplayAreas');
  extraDisplayAreas.innerHTML = '';
}
function sanlingqi(input) {
  const parts = delstr(input);
  const rows = Number(parts[0]);
  const rules = [1];
  const infoArray = ['充电枪数量'];
  for (let i = 0; i < rows; i++) {
    rules.push(7);
    infoArray.push(
      `第${i + 1}把枪四个参数（充电状态，充电电压,充电电流,充电电量`,
    );
  }
  return tongyong(input, rules, infoArray, 307);
}

function yilingyi(input) {
  const rules = [2, 2, 2, 2, 2, 1, 2, 2, 2, 24];
  const infoArray = [
    '通讯协议版本号',
    '主控软件版本号',
    '枪控软件版本号',
    '硬件版本',
    '白名单版本号',
    '白名单',
    '费率版本号',
    '模块功率',
    '模块限流',
    'ICCID',
  ];
  return tongyong(input, rules, infoArray);
}

function yilinger(input) {
  const rules = [2, 6, 48];
  const infoArray = ['结果', '时间校准', '二维码前缀'];
  return tongyong(input, rules, infoArray);
}

function erlingyi(input) {
  const rules = [1, 4];
  const infoArray = ['枪编号', '卡号'];
  return tongyong(input, rules, infoArray);
}

function erlinger(input) {
  const rules = [1, 2, 4, 4, 8];
  const infoArray = ['枪编号', '结果', '卡号', '金额', '订单流水号'];
  return tongyong(input, rules, infoArray);
}

function erlingsan(input) {
  const rules = [1, 17];
  const infoArray = ['枪编号', 'VIN'];
  return tongyong(input, rules, infoArray);
}

function erlingsi(input) {
  const rules = [1, 2, 17, 4, 4, 8];
  const infoArray = ['枪编号', '结果', 'VIN', '卡号', '金额', '订单流水号'];
  return tongyong(input, rules, infoArray);
}

function sanlingyi(input) {
  const rules = [
    1, 8, 4, 4, 1, 1, 1, 2, 2, 4, 4, 1, 1, 1, 4, 2, 2, 17, 1, 2, 2, 2, 6, 4,
  ];
  const infoArray = [
    '枪编号',
    '交易流水号',
    '卡号',
    '卡内余额',
    '启动方式',
    'BMS版本',
    '电池类型',
    '额定容量',
    '额定总电压',
    'BMS生产厂商',
    '电池组序号',
    '电池组生产日期年',
    '电池组生产日期月',
    '电池组生产日期日',
    '电池组充电次数',
    '电池电压',
    '允许温度',
    'VIN',
    '开始SOC',
    '绝缘检测电压',
    '正极阻值',
    '负极阻值',
    '开始时间',
    '电表度数',
  ];
  return tongyong(input, rules, infoArray);
}

function sanlinger(input) {
  const rules = [1, 8];
  const infoArray = ['枪编号', '交易流水号'];
  return tongyong(input, rules, infoArray);
}

function sanlingsi(input) {
  const rules = [1, 8, 4, 1, 2, 2, 2, 2, 2, 4, 4, 1, 2, 2, 2, 2, 2];
  const infoArray = [
    '充电枪编号',
    '交易流水号',
    '卡号',
    '启动方式',
    '电压',
    '电流',
    '正极温度',
    '负极温度',
    '充电时长',
    '充电电量',
    '充电费用',
    'SOC',
    '需求电压',
    '需求电流',
    '单体最高温度',
    '单体最低温度',
    '单体最高电压',
  ];
  return tongyong(input, rules, infoArray);
}

function sanlingwu(input) {
  const rules = [
    2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
  ];
  const infoArray = [
    '主机进风温度',
    '主机出风温度',
    '模块1状态',
    '模块1温度',
    '模块2状态',
    '模块2温度',
    '模块3状态',
    '模块3温度',
    '模块4状态',
    '模块4温度',
    '模块5状态',
    '模块5温度',
    '模块6状态',
    '模块6温度',
    '模块7状态',
    '模块7温度',
    '模块8状态',
    '模块8温度',
    '模块9状态',
    '模块9温度',
    '模块10状态',
    '模块10温度',
  ];
  return tongyong(input, rules, infoArray);
}

function sanlingliu(input) {
  const rules = [1, 8, 1, 2, 2, 2, 4, 4];
  const infoArray = [
    '充电枪编号',
    '交易流水号',
    '启动方式',
    '电压',
    '电流',
    '充电时长',
    '充电功率',
    '充电费用',
  ];
  return tongyong(input, rules, infoArray);
}

function sanlingba(input) {
  const rules = [2, 6];
  const infoArray = ['结果', '反应时间'];
  return tongyong(input, rules, infoArray);
}

function sanlingjiu(input) {
  const rules = [1, 8, 2, 2, 2];
  const infoArray = ['充电枪编号', '交易流水号', '故障码', '参数1', '参数2'];
  return tongyong(input, rules, infoArray);
}

function sanyiling(input) {
  const rules = [1, 8];
  const infoArray = ['充电枪编号', '交易流水号'];
  return tongyong(input, rules, infoArray);
}

function silingyi(input) {
  const rules = [1, 6];
  const infoArray = ['枪编号', '补传开始时间'];
  return tongyong(input, rules, infoArray);
}

function silinger(input) {
  const rules = [1, 2];
  const infoArray = ['枪编号', '结果'];
  return tongyong(input, rules, infoArray);
}

function silingsan(input) {
  const rules = [
    1, 8, 4, 4, 4, 1, 6, 1, 4, 2, 1, 4, 17, 4, 1, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  ];
  const infoArray = [
    '充电枪编号',
    '交易流水号',
    '记录编号',
    '卡号',
    '卡内余额',
    '启动方式',
    '开始时间',
    '开始SOC',
    '开始电表读数',
    '充电时长',
    '结束SOC',
    '充电电量',
    'VIN',
    '费用',
    '停止原因',
    '故障码',
    '故障参数1',
    '故障参数2',
    'BST结束报文',
    '半时段电量1',
    '半时段电量2',
    '半时段电量3',
    '半时段电量4',
    '半时段电量5',
    '半时段电量6',
    '半时段电量7',
    '半时段电量8',
    '半时段电量9',
    '半时段电量10',
    '半时段电量11',
    '半时段电量12',
    '半时段电量13',
    '半时段电量14',
    '半时段电量15',
    '半时段电量16',
    '半时段电量17',
    '半时段电量18',
    '半时段电量19',
    '半时段电量20',
  ];
  return tongyong(input, rules, infoArray);
}

function silingsi(input) {
  const rules = [1, 4, 8];
  const infoArray = ['枪编号', '交易流水号', '记录编号'];
  return tongyong(input, rules, infoArray);
}

function wulingyi(input) {
  const rules = [1, 8, 4, 4, 1, 1, 1, 2];
  const infoArray = [
    '枪编号',
    '交易流水号',
    '卡号',
    '余额',
    '启动方式',
    'BMS辅助电源电压',
    'SOC设定值',
    '金额设定值',
  ];
  return tongyong(input, rules, infoArray);
}

function wulinger(input) {
  const rules = [1, 2, 8];
  const infoArray = ['枪编号', '结果', '交易流水号'];
  return tongyong(input, rules, infoArray);
}

function wulingsan(input) {
  const rules = [1, 8];
  const infoArray = ['枪编号', '交易流水号'];
  return tongyong(input, rules, infoArray);
}

function wulingsi(input) {
  const rules = [1, 2, 8];
  const infoArray = ['枪编号', '结果', '交易流水号'];
  return tongyong(input, rules, infoArray);
}

function wulingwu(input) {
  const rules = [1, 2];
  const infoArray = ['枪编号', '电流参数'];
  return tongyong(input, rules, infoArray);
}

function wulingliu(input) {
  const rules = [1, 2];
  const infoArray = ['枪编号', '结果'];
  return tongyong(input, rules, infoArray);
}

function wulingqi(input) {
  const rules = [1, 2];
  const infoArray = ['重启类型', '秘钥参数'];
  return tongyong(input, rules, infoArray);
}

function wulingba(input) {
  const rules = [1];
  const infoArray = ['结果'];
  return tongyong(input, rules, infoArray);
}

function wulingjiu(input) {
  const rules = [1, 4];
  const infoArray = ['操作类型', '校验码'];
  return tongyong(input, rules, infoArray);
}

function wuyiling(input) {
  const rules = [1];
  const infoArray = ['结果'];
  return tongyong(input, rules, infoArray);
}

function liulingyi(input) {
  const rules = [1];
  const infoArray = ['标识符'];
  return tongyong(input, rules, infoArray);
}

function liulinger(input) {
  const rules = [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2];
  const infoArray = [
    '白名单状态',
    '白名单版本',
    '白名单记录数',
    '费率版本号',
    '模块功率',
    '模块限流',
    '主控版本号',
    '枪控1版本号',
    '枪控2版本号',
    '枪控3版本号',
    '枪控4版本号',
    '枪控5版本号',
  ];
  return tongyong(input, rules, infoArray);
}

function liuyiyi(input) {
  const rules = [2, 2];
  const infoArray = ['版本号', '索引'];
  return tongyong(input, rules, infoArray);
}

function liuyier(input) {
  const parts = delstr(input);
  const rows = Number(parts[1]);
  const rules = [2, 1];
  const infoArray = ['索引', '数量'];
  for (let i = 0; i < rows; i++) {
    rules.push(21);
    infoArray.push(`第${i + 1}张卡两个参数（VIN，卡号`);
  }

  return tongyong(input, rules, infoArray, 612);
}

function liuyisan(input) {
  const rules = [1, 2, 2];
  const infoArray = ['操作', '版本号', '记录数'];
  return tongyong(input, rules, infoArray);
}

function liuyisi(input) {
  const rules = [2];
  const infoArray = ['结果'];
  return tongyong(input, rules, infoArray);
}

function liueryi(input) {
  const rules = [1];
  const infoArray = [
    '枪编号',
    '版本号',
    '0点-0点30 电费',
    '0点-0点30 服务费',
    '0点30-1点 电费',
    '0点30-1点 服务费',
    '1点-1点30 电费',
    '1点-1点30 服务费',
    '1点30-2点 电费',
    '1点30-2点 服务费',
    '2点-2点30 电费',
    '2点-2点30 服务费',
    '2点30-3点 电费',
    '2点30-3点 服务费',
    '3点-3点30 电费',
    '3点-3点30 服务费',
    '3点30-4点 电费',
    '3点30-4点 服务费',
    '4点-4点30 电费',
    '4点-4点30 服务费',
    '4点30-5点 电费',
    '4点30-5点 服务费',
    '5点-5点30 电费',
    '5点-5点30 服务费',
    '5点30-6点 电费',
    '5点30-6点 服务费',
    '6点-6点30 电费',
    '6点-6点30 服务费',
    '6点30-7点 电费',
    '6点30-7点 服务费',
    '7点-7点30 电费',
    '7点-7点30 服务费',
    '7点30-8点 电费',
    '7点30-8点 服务费',
    '8点-8点30 电费',
    '8点-8点30 服务费',
    '8点30-9点 电费',
    '8点30-9点 服务费',
    '9点-9点30 电费',
    '9点-9点30 服务费',
    '9点30-10点 电费',
    '9点30-10点 服务费',
    '10点-10点30 电费',
    '10点-10点30 服务费',
    '10点30-11点 电费',
    '10点30-11点 服务费',
    '11点-11点30 电费',
    '11点-11点30 服务费',
    '11点30-12点 电费',
    '11点30-12点 服务费',
    '12点-12点30 电费',
    '12点-12点30 服务费',
    '12点30-13点 电费',
    '12点30-13点 服务费',
    '13点-13点30 电费',
    '13点-13点30 服务费',
    '13点30-14点 电费',
    '13点30-14点 服务费',
    '14点-14点30 电费',
    '14点-14点30 服务费',
    '14点30-15点 电费',
    '14点30-15点 服务费',
    '15点-15点30 电费',
    '15点-15点30 服务费',
    '15点30-16点 电费',
    '15点30-16点 服务费',
    '16点-16点30 电费',
    '16点-16点30 服务费',
    '16点30-17点 电费',
    '16点30-17点 服务费',
    '17点-17点30 电费',
    '17点-17点30 服务费',
    '17点30-18点 电费',
    '17点30-18点 服务费',
    '18点-18点30 电费',
    '18点-18点30 服务费',
    '18点30-19点 电费',
    '18点30-19点 服务费',
    '19点-19点30 电费',
    '19点-19点30 服务费',
    '19点30-20点 电费',
    '19点30-20点 服务费',
    '20点-20点30 电费',
    '20点-20点30 服务费',
    '20点30-21点 电费',
    '20点30-21点 服务费',
    '21点-21点30 电费',
    '21点-21点30 服务费',
    '21点30-22点 电费',
    '21点30-22点 服务费',
    '22点-22点30 电费',
    '22点-22点30 服务费',
    '22点30-23点 电费',
    '22点30-23点 服务费',
    '23点-23点30 电费',
    '23点-23点30 服务费',
    '23点30-24点 电费',
    '23点30-24点 服务费',
  ];
  for (let i = 0; i < infoArray.length - 1; i++) {
    rules.push(2);
  }
  return tongyong(input, rules, infoArray);
}

function liuerer(input) {
  const rules = [2, 2];
  const infoArray = ['结果', '版本号'];
  return tongyong(input, rules, infoArray);
}

function liusanyi(input) {
  const rules = [2, 2];
  const infoArray = ['模块功率', '限流值'];
  return tongyong(input, rules, infoArray);
}

function liusaner(input) {
  const rules = [2];
  const infoArray = ['结果'];
  return tongyong(input, rules, infoArray);
}

function liusanqi(input) {
  const rules = [4];
  const infoArray = ['桩编号'];
  return tongyong(input, rules, infoArray);
}

function liusanba(input) {
  const rules = [24, 20, 20, 1];
  const infoArray = ['ICCID', 'APN', 'IMSI', '信号强度'];
  return tongyong(input, rules, infoArray);
}

function qilingyi(input) {
  const rules = [1, 2, 6, 4, 2, 2, 2];
  const infoArray = [
    '升级标识',
    '版本号',
    '文件时间',
    '文件大小',
    '单包大小',
    '包数量',
    '文件校验',
  ];
  return tongyong(input, rules, infoArray);
}
